package com.ruoyi.app.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.app.domain.AppTeamType;
import com.ruoyi.app.domain.vo.AppTeamTypeVo;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.app.domain.bo.AppTeamApplyBo;
import com.ruoyi.app.domain.vo.AppTeamApplyVo;
import com.ruoyi.app.domain.AppTeamApply;
import com.ruoyi.app.mapper.AppTeamApplyMapper;
import com.ruoyi.app.service.IAppTeamApplyService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * APP拼团类型Service业务层处理
 *
 * @author heike
 * @date 2023-08-16
 */
@RequiredArgsConstructor
@Service
public class AppTeamApplyServiceImpl implements IAppTeamApplyService {

    private final AppTeamApplyMapper baseMapper;

    /**
     * 查询APP拼团类型
     */
    @Override
    public AppTeamApplyVo queryById(Long id) {
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询APP拼团类型列表
     */
    @Override
    public TableDataInfo<AppTeamApplyVo> queryPageList(AppTeamApplyBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<AppTeamApply> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(AppTeamApply::getCreateTime);
        Page<AppTeamApplyVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询APP拼团类型列表
     */
    @Override
    public List<AppTeamApplyVo> queryList(AppTeamApplyBo bo) {
        LambdaQueryWrapper<AppTeamApply> lqw = buildQueryWrapper(bo);
        lqw.orderByDesc(AppTeamApply::getCreateTime);
        return baseMapper.selectVoList(lqw);
    }

    /**
     * 查询APP拼团类型列表
     */
    @Override
    public List<AppTeamApplyVo> queryList(Long userID,String status) {
        LambdaQueryWrapper<AppTeamApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppTeamApply::getUserId,userID).ne(AppTeamApply::getStatus,status);
        wrapper.orderByDesc(AppTeamApply::getCreateTime);
        return baseMapper.selectVoList(wrapper);
    }

    @Override
    public AppTeamApplyVo queryByTeamAndUser(Long id, Long userID, String status) {
        LambdaQueryWrapper<AppTeamApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppTeamApply::getTeamId, id).eq(AppTeamApply::getUserId, userID)
            .eq(AppTeamApply::getStatus, status);
        return baseMapper.selectVoOne(wrapper);
    }

    private LambdaQueryWrapper<AppTeamApply> buildQueryWrapper(AppTeamApplyBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<AppTeamApply> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getUserId() != null, AppTeamApply::getUserId, bo.getUserId());
        lqw.eq(bo.getTeamId() != null, AppTeamApply::getTeamId, bo.getTeamId());
        lqw.eq(StringUtils.isNotBlank(bo.getStatus()), AppTeamApply::getStatus, bo.getStatus());
        return lqw;
    }

    /**
     * 新增APP拼团类型
     */
    @Override
    public Boolean insertByBo(AppTeamApplyBo bo) {
        AppTeamApply add = BeanUtil.toBean(bo, AppTeamApply.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改APP拼团类型
     */
    @Override
    public Boolean updateByBo(AppTeamApplyBo bo) {
        AppTeamApply update = BeanUtil.toBean(bo, AppTeamApply.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(AppTeamApply entity) {
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除APP拼团类型
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if (isValid) {
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }

    @Override
    public Boolean delect(Long id) {
        return baseMapper.deleteById(id) > 0;
    }

    @Override
    public Boolean insert(AppTeamApplyBo team) {
        return insertByBo(team);
    }

    @Override
    public Boolean updateByVo(AppTeamApplyVo vo) {
        AppTeamApply update = BeanUtil.toBean(vo, AppTeamApply.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    @Override
    public AppTeamApplyVo queryUser(Long id, Long userID, String type) {
        LambdaQueryWrapper<AppTeamApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppTeamApply::getUserId, userID).eq(AppTeamApply::getTeamId, id).eq(AppTeamApply::getStatus, type);
        return baseMapper.selectVoOne(wrapper);
    }

    @Override
    public AppTeamApplyVo queryUser(Long id, Long userID) {
        LambdaQueryWrapper<AppTeamApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppTeamApply::getUserId, userID).eq(AppTeamApply::getTeamId, id);
        return baseMapper.selectVoOne(wrapper);
    }

    @Override
    public Boolean join(Long id, Long userID) {
        LambdaQueryWrapper<AppTeamApply> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AppTeamApply::getUserId, userID).eq(AppTeamApply::getTeamId, id)
            .ne(AppTeamApply::getStatus, Constants.TWO);
        return baseMapper.selectCount(wrapper) > 0;
    }
}
